package com.enniswu.weitter.moment.postlist;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.enniswu.weitter.R;
import com.enniswu.weitter.base.BaseFragment;
import com.enniswu.weitter.entity.Post;
import com.enniswu.weitter.moment.MomentFragment;
import com.enniswu.weitter.widget.adapter.PostAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;

public class PostListFragment extends BaseFragment<PostListContract.Presenter> implements PostListContract.View<PostListContract.Presenter>, OnRefreshListener, OnLoadMoreListener {

    public static final int ORIENTATION_PUBLIC = 0;
    public static final int ORIENTATION_FOLLOWING = 1;
    @BindView(R.id.rl_moment)
    SmartRefreshLayout rlMoment;
    @BindView(R.id.rv_moment)
    RecyclerView rvMoment;
    private int orientation;
    private PostAdapter adapter;
    private List<Post> posts;

    public static PostListFragment newInstance(int orientation) {
        Bundle bundle = new Bundle();
        bundle.putInt("orientation", orientation);
        PostListFragment fragment = new PostListFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            orientation = getArguments().getInt("orientation");
        }
    }

    @Override
    protected int setContentView() {
        return R.layout.fragment_post_list;
    }

    @Override
    protected void onCreateView(View contentView) {
        posts = Collections.synchronizedList(new ArrayList<Post>());
        adapter = new PostAdapter(posts);
        rvMoment.setLayoutManager(new LinearLayoutManager(getContext()));
        rvMoment.setAdapter(adapter);
        rlMoment.setOnRefreshListener(this);
        rlMoment.setOnLoadMoreListener(this);
        new PostListPresenter<>(this, 0, orientation);
        presenter.start();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == MomentFragment.REQUEST_POST && resultCode == Activity.RESULT_OK) {
            if (data.getIntExtra("type", -1) == orientation)
                refresh();
        }
    }

    @Override
    public void notifyDataSetChanged() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void notifyDataInserted(final int count) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyItemRangeInserted(posts.size() - count, count);
                adapter.notifyItemRangeChanged(posts.size() - count, count);
            }
        });
    }

    @Override
    public void notifyItemUserChanged(Post post) {
        final int index = posts.indexOf(post);
        if (index < 0)
            return;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyItemChanged(index);
            }
        });
    }

    @Override
    public void addPosts(List<Post> posts) {
        this.posts.addAll(posts);
    }

    @Override
    public void clearPosts() {
        posts.clear();
    }

    @Override
    public void refresh() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                rlMoment.autoRefresh();
            }
        });
    }

    @Override
    public void finishLoadData() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                rlMoment.finishRefresh();
                rlMoment.finishLoadMore();
            }
        });
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        presenter.getPosts(posts.size() + 1);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        presenter.getPosts(1);
    }
}
